package br.poli.ecomp.tcc.stimultimidia.business
{
	 
	import br.poli.ecomp.tcc.stimultimidia.model.vo.TelaVO;
	import br.poli.ecomp.tcc.stimultimidia.model.vo.UsuarioVO;
	import br.poli.ecomp.tcc.stimultimidia.notification.RemoteConnectionNotification;
	
	import com.demonsters.debugger.MonsterDebugger;
	
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.NetConnection;
	import flash.net.registerClassAlias;
	import flash.utils.setTimeout;
	
	import org.puremvc.as3.patterns.proxy.Proxy;

	public class RemoteConnection extends Proxy
	{ 
		public static var _urlServer:String;
		private static var _instance:RemoteConnection;
		private var conn:NetConnection;
		 
		 
		 

		public static function getInstance(url:String = null):RemoteConnection{
			if( _instance == null ){ 
				_instance = new RemoteConnection(url);
			}  
			 
			return _instance; 
		}
		
		
		
		public function RemoteConnection(url:String = null){
			var user:UsuarioVO; 
			var tela:TelaVO;
			 
			 		 
			
			 registerClassAlias("br.poli.ecomp.tcc.stimultimidia.model.vo.TelaVO", TelaVO);
			 registerClassAlias("br.poli.ecomp.tcc.stimultimidia.model.vo.UsuarioVO", UsuarioVO);
			 
			conn = new NetConnection();
			
			conn.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			conn.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			conn.addEventListener(IOErrorEvent.IO_ERROR , ioErrorHandler);
			conn.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			
			conn.connect( url );
			
			    
		}
		
		 
		
		
		private function onErro(e:Event):void
		{
			sendNotification(RemoteConnectionNotification.ERROR_REMOTE,e );
		}
		
		
		public function get conexao():NetConnection{
			return this.conn;
		} 
		
		private  function netStatusHandler(event:NetStatusEvent):void {
			switch (event.info.code) {
				case "NetConnection.Connect.Success":
					
					//trace("Connect.Success");
					break;
				case "NetStream.Play.StreamNotFound":
					//trace("Stream not found: " );
					break;
			}
			sendNotification(RemoteConnectionNotification.NETSTATUS,event);
		}
		
		
		
		private  function securityErrorHandler(event:SecurityErrorEvent):void {
			//trace("securityErrorHandler: "  );
			//trace(event);
			sendNotification(RemoteConnectionNotification.SECURITYERROR,event);
		}
		
		
		private  function httpStatusHandler( e:HTTPStatusEvent ):void { 
			//trace("httpStatusHandler:" + e); 
			sendNotification(RemoteConnectionNotification.HTTPSTATUS,e);
		}
		
		private  function ioErrorHandler( e:IOErrorEvent ):void { 
			//trace("ORNLoader:ioErrorHandler: " + e); 
			sendNotification(RemoteConnectionNotification.IOERROR,e);
		}
		
		
		
		
		 
	}
	 
}